package com.situ.player.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.Image;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.situ.player.R;
import com.situ.player.service.MusicService;

import java.util.ArrayList;

public class MusicPlayActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "MusicPlayActivity";

    // 当前正在播放的歌曲的路径
    private String mPath;
    // 当前目录下面所有的MP3歌曲
    private ArrayList<String> mMp3List;
    // 当前正在播放歌曲在集合中索引
    private Integer mCurrentMusinIndex;
    private MusicService.MusicControll mMusicContrll;

    private static SeekBar mSeekBar;
    private static TextView mTvProgress;
    private static TextView mTvTotal;
    private ImageView mIvPrevious;
    private ImageView mIvPlay;
    private ImageView mIvNext;

    public static Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            // 总时长
            int duration = bundle.getInt("duration");
            // 当前播放时长
            int currentPosition = bundle.getInt("currentPosition");
            mSeekBar.setMax(duration);
            mSeekBar.setProgress(currentPosition);

            // 显示总时长
            int minute = duration / 1000 / 60;
            int second = duration / 1000 % 60;
            mTvTotal.setText(minute + ":" + second);

            // 显示播放时长
            int currentMinute = currentPosition / 1000 / 60;
            int currentSecond = currentPosition / 1000 % 60;
            mTvProgress.setText(currentMinute + ":" + currentSecond);
        }
    };
    
    private ServiceConnection mConnection = new ServiceConnection() {
        // 当Service绑定成功之后回调这个方法
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected: ");
            mMusicContrll = (MusicService.MusicControll) service;
            mMusicContrll.init(mPath);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);
        initView();
        initData();
        bindMyService();
    }

    private void bindMyService() {
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

    private void initData() {
        Intent intent = getIntent();
        mPath = intent.getStringExtra("path");
        Log.d(TAG, "initData: path: " + mPath);
        mMp3List = (ArrayList<String>) intent.getSerializableExtra("mp3List");
        mCurrentMusinIndex = mMp3List.indexOf(mPath);
    }

    private void initView() {
        mSeekBar = (SeekBar) findViewById(R.id.seekBar);
        mTvProgress = (TextView) findViewById(R.id.tv_progress);
        mTvTotal = (TextView) findViewById(R.id.tv_total);
        mIvPrevious = (ImageView) findViewById(R.id.iv_previous);
        mIvPlay = (ImageView) findViewById(R.id.iv_play);
        mIvNext = (ImageView) findViewById(R.id.iv_next);
        mIvPrevious.setOnClickListener(this);
        mIvPlay.setOnClickListener(this);
        mIvNext.setOnClickListener(this);

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            // 拖动停止时候触发
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 得到当前拖动停止的位置
                int progress = seekBar.getProgress();
                mMusicContrll.seekTo(progress);
            }


            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            // 开始拖动时候触发
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }


        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_previous:
                if (mCurrentMusinIndex == 0) {
                    Toast.makeText(this, "这已经是第一首", Toast.LENGTH_SHORT).show();
                    return;
                }
                mCurrentMusinIndex--;
                mMusicContrll.init(mMp3List.get(mCurrentMusinIndex));
                break;
            case R.id.iv_play:
                if (mMusicContrll.isPlaying()) {
                    mMusicContrll.pause();
                    mIvPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_play));
                } else {
                    mMusicContrll.play();
                    mIvPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_pause));
                }
                break;
            case R.id.iv_next:
                if (mCurrentMusinIndex == mMp3List.size() - 1) {
                    Toast.makeText(this, "这已经是最后一首", Toast.LENGTH_SHORT).show();
                    return;
                }
                mCurrentMusinIndex++;
                mMusicContrll.init(mMp3List.get(mCurrentMusinIndex));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMusicContrll.pause();
        // 解绑Service
        unbindService(mConnection);
        Intent intent = new Intent(this, MusicService.class);
        stopService(intent);
    }
}
